package com.czl.module_work.fragment

import android.graphics.Color
import android.graphics.Matrix
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.PercentBean
import com.czl.base.data.bean.WorkOrderBean
import com.czl.module_work.R
import com.czl.module_work.viewModel.WorkViewModel
import com.czl.module_work.BR
import com.czl.module_work.adapter.MyOrderAdapter
import com.czl.module_work.adapter.WorkTimeAdapter
import com.czl.module_work.databinding.WorkFragmentWorkBinding
import com.czl.module_work.viewModel.MyOrderDetailViewModel
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.XAxis

import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.Description

import com.github.mikephil.charting.formatter.IAxisValueFormatter

import com.github.mikephil.charting.data.BarData

import com.github.mikephil.charting.data.BarDataSet

import com.github.mikephil.charting.data.BarEntry

import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.formatter.ValueFormatter
import kotlin.random.Random
import com.github.mikephil.charting.components.YAxis
import java.util.*
import kotlin.collections.ArrayList
import com.github.mikephil.charting.utils.ViewPortHandler

import com.github.mikephil.charting.formatter.IValueFormatter

import com.github.mikephil.charting.interfaces.datasets.IBarDataSet


/**
 * 工单
 */
@Route(path = AppConstants.Router.Work.F_WORK)
class WorkFragment : BaseFragment<WorkFragmentWorkBinding, WorkViewModel>() {

    private lateinit var mAdapter: WorkTimeAdapter
    override fun initContentView(): Int {
        return R.layout.work_fragment_work
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("工单分析")
        loadData()
        initAdapter()
    }

    private fun loadData() {
        viewModel.getUserRooms()
        viewModel.workOrderPercent()
        viewModel.workOrderTime()
    }

    private fun initAdapter() {
        mAdapter = WorkTimeAdapter()

        binding.ryCommon.apply {
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        viewModel.uc.loadChatEvent.observe(this, Observer {
            it ?: return@Observer
            binding.barchart1.visibility = View.VISIBLE
            binding.barchart2.visibility = View.GONE
            binding.barchart3.visibility = View.GONE
            initChart(it)
        })
        viewModel.uc.loadChat2Event.observe(this, Observer {
            it ?: return@Observer
            binding.barchart1.visibility = View.GONE
            binding.barchart2.visibility = View.VISIBLE
            binding.barchart3.visibility = View.GONE
            initChart2(it)
        })
        viewModel.uc.loadChat3Event.observe(this, Observer {
            it ?: return@Observer
            binding.barchart1.visibility = View.GONE
            binding.barchart2.visibility = View.GONE
            binding.barchart3.visibility = View.VISIBLE
            initChart3(it)
        })
        viewModel.uc.loadOrderTimeEvent.observe(this, Observer {
            it ?: return@Observer
            mAdapter.addData(it)
        })
    }

    private fun initChart(list: List<PercentBean>) {
        binding.barchart1.getLegend()
            .setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER)
        binding.barchart1.getLegend().setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM)
        val barEntryList: MutableList<BarEntry> = ArrayList()
        val barEntryList2: MutableList<BarEntry> = ArrayList()
        val barEntryList3: MutableList<BarEntry> = ArrayList()
        val barEntryList4: MutableList<BarEntry> = ArrayList()
        val barEntryList5: MutableList<BarEntry> = ArrayList()
        list.forEachIndexed { index, it ->
            barEntryList.add(BarEntry(index.toFloat(), it.baoyang.toFloat()))
            barEntryList2.add(BarEntry(index.toFloat(), it.xunjian.toFloat()))
            barEntryList3.add(BarEntry(index.toFloat(), it.baojing.toFloat()))
            barEntryList4.add(BarEntry(index.toFloat(), it.weixiu.toFloat()))
            barEntryList5.add(BarEntry(index.toFloat(), it.tousu.toFloat()))
//
//            barEntryList.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
//            barEntryList2.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
//            barEntryList3.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
//            barEntryList4.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
        }

        val barDataSet = BarDataSet(barEntryList, "保养工单")
        val barDataSet2 = BarDataSet(barEntryList2, "巡检工单")
        val barDataSet3 = BarDataSet(barEntryList3, "报警工单")
        val barDataSet4 = BarDataSet(barEntryList4, "维修工单")
        val barDataSet5 = BarDataSet(barEntryList5, "投诉工单")

        barDataSet.color = Color.parseColor("#70CA9E")
        barDataSet2.color = Color.parseColor("#6BB1FF")
        barDataSet3.color = Color.parseColor("#F29968")
        barDataSet4.color = Color.parseColor("#FF3B30")
        barDataSet5.color = Color.parseColor("#666666")
        barDataSet.setDrawValues(false)
        barDataSet2.setDrawValues(false)
        barDataSet3.setDrawValues(false)
        barDataSet4.setDrawValues(false)
        barDataSet5.setDrawValues(false)

        val barData = BarData(
            barDataSet, barDataSet2, barDataSet3, barDataSet4, barDataSet5
        )
        binding.barchart1.xAxis.valueFormatter = object : ValueFormatter() {
            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                val pos = value.toInt() % list.size
                return list[pos].label
            }
        }
        val ratio = list.size.toFloat() / 10f
        binding.barchart1.zoom(ratio, 1f, 0f, 0f) //显示的时候是按照多大的比率缩放显示  1f表示不放大缩小


        binding.barchart1.axisLeft.setYOffset(0f) //这样会向下偏移 50%

        binding.barchart1.axisLeft.setLabelCount(8, false) //设置间距

        binding.barchart1.getAxisLeft().setValueFormatter(object : ValueFormatter() {
            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                return value.toInt().toString()
            }
        })

        binding.barchart1.xAxis.textSize = 8f
        binding.barchart1.xAxis.textColor = Color.BLACK
        binding.barchart1.xAxis.axisMinimum = 0f
//        if (list.size < 12) {
        binding.barchart1.xAxis.setLabelCount(8, true)
//        } else {
//            binding.barchart1.xAxis.setLabelCount(13, true)
//        }
        binding.barchart1.xAxis.position = XAxis.XAxisPosition.BOTTOM
        binding.barchart1.xAxis.setCenterAxisLabels(true)
        binding.barchart1.xAxis.axisLineColor = Color.parseColor("#E5E5E5")
        binding.barchart1.axisLeft.axisLineColor = Color.parseColor("#3ECC98")
        binding.barchart1.xAxis.setDrawAxisLine(false) //横线
        binding.barchart1.xAxis.setDrawGridLines(false) //垂线
        binding.barchart1.axisRight.setDrawAxisLine(false)
        binding.barchart1.axisRight.isEnabled = false
        binding.barchart1.setScaleEnabled(false);//设置是否可以缩放
        binding.barchart1.setTouchEnabled(true);//设置是否可以触摸
        binding.barchart1.isDragEnabled = true
        val description = Description()

        description.isEnabled = false

        binding.barchart1.description = description


        /*float groupSpace = 0.12f; //柱状图组之间的间距
          float barSpace =(float)((1 - 0.12) / yListValue.size() / 10); // x4 DataSet
          float barWidth =(float)((1 - 0.12) / yListValue.size() / 10 * 9); // x4 DataSet*/

        val groupSpace = 0.12f
        val barSpace = 0f
        val barWidth = (1 - 0.12).toFloat() / 5f / 10f * 9f + (1 - 0.12).toFloat() / 5f / 10f

        barData.barWidth = barWidth
        binding.barchart1.data = barData //先设置数据
//然后再设置分组柱状图
        binding.barchart1.axisLeft.axisMinimum = 0f
        binding.barchart1.axisLeft.axisMaximum = 30f
        binding.barchart1.xAxis.axisMaximum =
            0 + binding.barchart1.getBarData().getGroupWidth(groupSpace, barSpace) * list.size;
        binding.barchart1.groupBars(0f, groupSpace, barSpace)
        binding.barchart1.setDrawValueAboveBar(false);//这里设置为true每一个直方图的值就会显示在直方图的顶部
        binding.barchart1.description.isEnabled = false;//设置描述不显示
        val mMatrix = Matrix();
        mMatrix.postScale(list.size.toFloat() / 7f, 1f);
        binding.barchart1.viewPortHandler.refresh(mMatrix, binding.barchart1, true);
        binding.barchart1.animateY(1000);
        binding.barchart1.invalidate()
    }

    private fun initChart3(list: List<PercentBean>) {
        binding.barchart3.getLegend()
            .setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER)
        binding.barchart3.getLegend().setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM)
        val barEntryList: MutableList<BarEntry> = ArrayList()
        val barEntryList2: MutableList<BarEntry> = ArrayList()
        val barEntryList3: MutableList<BarEntry> = ArrayList()
        val barEntryList4: MutableList<BarEntry> = ArrayList()
        val barEntryList5: MutableList<BarEntry> = ArrayList()
        list.forEachIndexed { index, it ->
            barEntryList.add(BarEntry(index.toFloat(), it.baoyang.toFloat()))
            barEntryList2.add(BarEntry(index.toFloat(), it.xunjian.toFloat()))
            barEntryList3.add(BarEntry(index.toFloat(), it.baojing.toFloat()))
            barEntryList4.add(BarEntry(index.toFloat(), it.weixiu.toFloat()))
            barEntryList5.add(BarEntry(index.toFloat(), it.tousu.toFloat()))
//            barEntryList.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
//            barEntryList2.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
//            barEntryList3.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
//            barEntryList4.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
        }

        val barDataSet = BarDataSet(barEntryList, "保养工单")
        val barDataSet2 = BarDataSet(barEntryList2, "巡检工单")
        val barDataSet3 = BarDataSet(barEntryList3, "报警工单")
        val barDataSet4 = BarDataSet(barEntryList4, "维修工单")
        val barDataSet5 = BarDataSet(barEntryList5, "投诉工单")

        barDataSet.color = Color.parseColor("#70CA9E")
        barDataSet2.color = Color.parseColor("#6BB1FF")
        barDataSet3.color = Color.parseColor("#F29968")
        barDataSet4.color = Color.parseColor("#FF3B30")
        barDataSet5.color = Color.parseColor("#666666")
        barDataSet.setDrawValues(false)
        barDataSet2.setDrawValues(false)
        barDataSet3.setDrawValues(false)
        barDataSet4.setDrawValues(false)
        barDataSet5.setDrawValues(false)


        val barData = BarData(
            barDataSet, barDataSet2, barDataSet3, barDataSet4, barDataSet5
        )
        binding.barchart3.xAxis.valueFormatter = object : ValueFormatter() {
            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                val absValue = Math.abs(value).toInt()
                return list.get(absValue % list.size).label
            }
        }
        val ratio = list.size.toFloat() / 10f
        binding.barchart3.zoom(ratio, 1f, 0f, 0f) //显示的时候是按照多大的比率缩放显示  1f表示不放大缩小

        binding.barchart3.axisLeft.setYOffset(0f) //这样会向下偏移 50%

        binding.barchart3.axisLeft.setLabelCount(8, false) //设置间距

        binding.barchart3.getAxisLeft().setValueFormatter(object : ValueFormatter() {
            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                return value.toInt().toString()
            }
        })

        binding.barchart3.xAxis.textSize = 8f
        binding.barchart3.xAxis.textColor = Color.BLACK
        binding.barchart3.xAxis.axisMinimum = 0f
//        if (list.size < 12) {
        binding.barchart3.xAxis.setLabelCount(8, true)
//        } else {
//            binding.barchart3.xAxis.setLabelCount(13, true)
//        }
        binding.barchart3.xAxis.position = XAxis.XAxisPosition.BOTTOM
        binding.barchart3.xAxis.setCenterAxisLabels(true)
        binding.barchart3.xAxis.axisLineColor = Color.parseColor("#E5E5E5")
        binding.barchart3.axisLeft.axisLineColor = Color.parseColor("#3ECC98")
        binding.barchart3.xAxis.setDrawAxisLine(false) //横线
        binding.barchart3.xAxis.setDrawGridLines(false) //垂线
        binding.barchart3.axisRight.setDrawAxisLine(false)
        binding.barchart3.axisRight.isEnabled = false
        binding.barchart3.setScaleEnabled(false);//设置是否可以缩放
        binding.barchart3.setTouchEnabled(true);//设置是否可以触摸
        binding.barchart3.isDragEnabled = true
        val description = Description()
        description.isEnabled = false
        binding.barchart3.description = description


        /*  float groupSpace = 0.12f; //柱状图组之间的间距
          float barSpace =(float)((1 - 0.12) / yListValue.size() / 10); // x4 DataSet
          float barWidth =(float)((1 - 0.12) / yListValue.size() / 10 * 9); // x4 DataSet*/

        val groupSpace = 0.12f
        val barSpace = 0f
        val barWidth = (1 - 0.12).toFloat() / 5f / 10f * 9f + (1 - 0.12).toFloat() / 5f / 10f

        barData.barWidth = barWidth
        binding.barchart3.data = barData //先设置数据
//然后再设置分组柱状图
        binding.barchart3.axisLeft.axisMinimum = 0f
        binding.barchart3.axisLeft.axisMaximum = 30f
        binding.barchart3.xAxis.axisMaximum =
            0 + binding.barchart3.getBarData().getGroupWidth(groupSpace, barSpace) * list.size;
        binding.barchart3.groupBars(0f, groupSpace, barSpace)
        binding.barchart3.setDrawValueAboveBar(false);//这里设置为true每一个直方图的值就会显示在直方图的顶部
        binding.barchart3.description.isEnabled = false;//设置描述不显示
        val mMatrix = Matrix();
        mMatrix.postScale(list.size.toFloat() / 7f, 1f);
        binding.barchart3.viewPortHandler.refresh(mMatrix, binding.barchart3, true);
        binding.barchart3.animateY(1000);
        binding.barchart3.invalidate()



    }


    private fun initChart2(list: List<PercentBean>) {
        binding.barchart2.getLegend()
            .setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER)
        binding.barchart2.getLegend().setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM)
        val barEntryList: MutableList<BarEntry> = ArrayList()
        val barEntryList2: MutableList<BarEntry> = ArrayList()
        val barEntryList3: MutableList<BarEntry> = ArrayList()
        val barEntryList4: MutableList<BarEntry> = ArrayList()
        val barEntryList5: MutableList<BarEntry> = ArrayList()
        list.forEachIndexed { index, it ->
            barEntryList.add(BarEntry(index.toFloat(), it.baoyang.toFloat()))
            barEntryList2.add(BarEntry(index.toFloat(), it.xunjian.toFloat()))
            barEntryList3.add(BarEntry(index.toFloat(), it.baojing.toFloat()))
            barEntryList4.add(BarEntry(index.toFloat(), it.weixiu.toFloat()))
            barEntryList5.add(BarEntry(index.toFloat(), it.tousu.toFloat()))
//
//            barEntryList.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
//            barEntryList2.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
//            barEntryList3.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
//            barEntryList4.add(BarEntry(index.toFloat(), (15..30).random().toFloat()))
        }

        val barDataSet = BarDataSet(barEntryList, "保养工单")
        val barDataSet2 = BarDataSet(barEntryList2, "巡检工单")
        val barDataSet3 = BarDataSet(barEntryList3, "报警工单")
        val barDataSet4 = BarDataSet(barEntryList4, "维修工单")
        val barDataSet5 = BarDataSet(barEntryList5, "投诉工单")

        barDataSet.color = Color.parseColor("#70CA9E")
        barDataSet2.color = Color.parseColor("#6BB1FF")
        barDataSet3.color = Color.parseColor("#F29968")
        barDataSet4.color = Color.parseColor("#FF3B30")
        barDataSet5.color = Color.parseColor("#666666")
        barDataSet.setDrawValues(false)
        barDataSet2.setDrawValues(false)
        barDataSet3.setDrawValues(false)
        barDataSet4.setDrawValues(false)
        barDataSet5.setDrawValues(false)


        val barData = BarData(
            barDataSet, barDataSet2, barDataSet3, barDataSet4, barDataSet5
        )
        binding.barchart2.xAxis.valueFormatter = object : ValueFormatter() {
            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                return list.get(Math.abs(value).toInt() % list.size).label;
            }
        }
        val ratio = list.size.toFloat() / 10f
        binding.barchart2.zoom(ratio, 1f, 0f, 0f) //显示的时候是按照多大的比率缩放显示  1f表示不放大缩小

        binding.barchart2.axisLeft.setYOffset(0f) //这样会向下偏移 50%

        binding.barchart2.axisLeft.setLabelCount(8, false) //设置间距

        binding.barchart2.getAxisLeft().setValueFormatter(object : ValueFormatter() {
            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                return value.toInt().toString()
            }
        })

        binding.barchart2.xAxis.textSize = 8f
        binding.barchart2.xAxis.textColor = Color.BLACK
        binding.barchart2.xAxis.axisMinimum = 0f
//        if (list.size < 12) {
        binding.barchart2.xAxis.setLabelCount(8, true)
//        } else {
//            binding.barchart2.xAxis.setLabelCount(13, true)
//        }
        binding.barchart2.xAxis.position = XAxis.XAxisPosition.BOTTOM
        binding.barchart2.xAxis.setCenterAxisLabels(true)
        binding.barchart2.xAxis.axisLineColor = Color.parseColor("#E5E5E5")
        binding.barchart2.axisLeft.axisLineColor = Color.parseColor("#3ECC98")
        binding.barchart2.xAxis.setDrawAxisLine(false) //横线
        binding.barchart2.xAxis.setDrawGridLines(false) //垂线
        binding.barchart2.axisRight.setDrawAxisLine(false)
        binding.barchart2.axisRight.isEnabled = false
        binding.barchart2.setScaleEnabled(false);//设置是否可以缩放
        binding.barchart2.setTouchEnabled(true);//设置是否可以触摸
        binding.barchart2.isDragEnabled = true
        val description = Description()
        description.isEnabled = false
        binding.barchart2.description = description


        /*  float groupSpace = 0.12f; //柱状图组之间的间距
          float barSpace =(float)((1 - 0.12) / yListValue.size() / 10); // x4 DataSet
          float barWidth =(float)((1 - 0.12) / yListValue.size() / 10 * 9); // x4 DataSet*/

        val groupSpace = 0.12f
        val barSpace = 0f
        val barWidth = (1 - 0.12).toFloat() / 5f / 10f * 9f + (1 - 0.12).toFloat() / 5f / 10f

        barData.barWidth = barWidth
        binding.barchart2.data = barData //先设置数据
//然后再设置分组柱状图
        binding.barchart2.axisLeft.axisMinimum = 0f
        binding.barchart2.axisLeft.axisMaximum = 30f
        binding.barchart2.xAxis.axisMaximum =
            0 + binding.barchart2.getBarData().getGroupWidth(groupSpace, barSpace) * list.size;
        binding.barchart2.groupBars(0f, groupSpace, barSpace)
        binding.barchart2.setDrawValueAboveBar(false);//这里设置为true每一个直方图的值就会显示在直方图的顶部
        binding.barchart2.description.isEnabled = false;//设置描述不显示
        val mMatrix = Matrix();
        mMatrix.postScale(list.size.toFloat() / 7f, 1f);
        binding.barchart2.viewPortHandler.refresh(mMatrix, binding.barchart2, true);
        binding.barchart2.animateY(1000);
        binding.barchart2.invalidate()
    }
}